package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.SysResourceMapper;
import com.example.pojo.SysResource;
import com.example.pojo.SysRole;
import com.example.mapper.SysRoleMapper;
import com.example.service.ISysResourceService;
import com.example.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-02-16
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysResourceMapper sysResourceMapper;

    @Override
    public Page<RoleVo> queryByCondition(Page<RoleVo> page, Map<String, Object> map) {
        QueryWrapper<RoleVo> wrapper = new QueryWrapper<>();
        wrapper.apply(" sr.del_flag=0 ")
                .like(!ObjectUtils.isEmpty(map.get("name")),"sr.name",map.get("name"))
                .eq(!ObjectUtils.isEmpty(map.get("officeId")),"so.id",map.get("officeId"));
        baseMapper.queryByCondition(page,wrapper);
        for (RoleVo record : page.getRecords()) {
            List<SysResource> sysResources = sysResourceMapper.queryByRoleId(record.getId());
            record.setSysResources(sysResources);
        }

        return page;
    }
}
